package keter.rabbitmq.direct;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Send {

  private final static String QUEUE_NAME = "hello";
  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("192.168.1.100");
    //factory.setPort(5672);//default port
    factory.setUsername("haha");
    factory.setPassword("haha");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(
            QUEUE_NAME,           //队列名称
            false,        //是否持久化
            false,       //是否排他
            false,     //不再使用时该队列是否自动删除
            null);
    String message = "Hello World!";
    channel.basicPublish(
            "",   //默认交换器
            QUEUE_NAME,    //路由键：使用“默认”即direct交换器时，ROUTING_KEY为QUEUE_NAME
            null,
            message.getBytes("UTF-8")//消息
    );
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();
    connection.close();
  }
}
